package com.leoman.controller.backend;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.entity.Mind;
import com.leoman.service.CommentService;
import com.leoman.service.MindService;
import com.leoman.service.PraiseService;
import com.leoman.utils.DateUtils;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 谈心管理
 * Created by 涂奕恒 on 2016/12/6.
 */
@Controller
@RequestMapping(value = "backend/mind")
public class MindController extends CommonController {

    @Autowired
    private MindService mindService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private PraiseService praiseService;

    @RequestMapping("index")
    public String index() {
        return "backend/谈心列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     String title,
                     String userName,
                     String startTime,
                     String endTime,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<Mind> page = mindService.pageByParams(title, userName, startTime, endTime, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("add")
    public String add(ModelMap model, Integer mindId) {
        if (null != mindId) {
            Mind mind = mindService.getById(mindId);
            mind.setContent(mind.getContent().replaceAll("&lt", "<").replaceAll("&gt", ">"));
            mind.setCommentNum(commentService.findListBySourceIdAndType(mindId, Constant.SOURCE_TYPE_MIND).size());
            mind.setPraiseNum(praiseService.findListBySourceIdAndType(mindId, Constant.SOURCE_TYPE_MIND).size());
            model.addAttribute("mind", mind);
            model.addAttribute("createTime", DateUtils.longToStringWithFormat(mind.getCreateTime(), "yyyy-MM-dd hh:mm:ss"));

            return "backend/谈心详情";
        } else {
            return "backend/添加谈心";
        }
    }

    /**
     * 置顶谈心
     *
     * @param mindId 谈心ID
     */
    @RequestMapping("/toTop")
    @ResponseBody
    public Integer toTop(Integer mindId) {
        try {
            return mindService.toTop(mindId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 添加or修改谈心
     */
    @RequestMapping("/save")
    @ResponseBody
    public Integer save(String title, String txtVal, String content, String openMoney, MultipartRequest multipartRequest) {
        try {
            return mindService.saveMind(title, txtVal, content, openMoney, multipartRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除谈心
     *
     * @param mindId 谈心ID
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Integer mindId) {
        try {
            mindService.deleteById(mindId);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量删除谈心
     *
     * @param ids 谈心ID
     * @return
     */
    @RequestMapping("/batchDelete")
    @ResponseBody
    public Integer batchDelete(String ids) {
        try {
            return mindService.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
